
//
// 110530 - AH - (Garage) Created.
// 110602 - AH - Missing java doc.
//

package org.soekrat.gui.swing.studio;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.event.SoekratChangeEvent;
import org.soekrat.engine.event.SoekratChangeListener;

/**
 * <p>
 *   Panel to edit or just to views title of conversation.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@SuppressWarnings("serial")
public final class ConversationTitleEditor extends JPanel 
{
  private final Soekrat2Engine m_engine;
  
  private final JLabel m_label;
  
  private final JTextField m_textField;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param se       {@link Soekrat2Engine} interfaced.
   * @param editable {@code true} if should be allowed to edit name and 
   *                 {@code false} if should only view name.
   */
  public ConversationTitleEditor(Soekrat2Engine se, boolean editable)
  {
    if (se==null)
    {
      throw new NullPointerException("se");
    }
    
    m_engine=se;
    
    setLayout(new BorderLayout());
    
    String title=se.getConversationTitle();
    
    if (editable)
    {
      m_label=null;
      m_textField=new JTextField(30);
      m_textField.setText(title);
      m_textField.setForeground(Color.RED);
      add(m_textField, BorderLayout.CENTER);
      
      m_textField.addActionListener
      (
        new ActionListener() 
        {
          public void actionPerformed(ActionEvent e)
          {
            String text=m_textField.getText();
            m_engine.setConversationTitle(text);
          
          }
        }
      );
    }
    else
    {
      m_textField=null;
      m_label=new JLabel(title);
      m_label.setForeground(Color.RED);
      add(m_label, BorderLayout.CENTER);
    }
    
    m_engine.addSoekratChangeListener
    (
      new SoekratChangeListener() 
      {
        public void changed(SoekratChangeEvent e) 
        {
          String text=m_engine.getConversationTitle();
          if (m_label!=null) m_label.setText(text);
          else               m_textField.setText(text);
        }
      }
    );
  }
  
}
